package leetcode.offer_two_special.linked;

/**
 * @author mazouri
 * @create 2021-09-06 21:38
 */
public class _21_removeNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) return null;

        //虚拟头结点
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        ListNode slow = dummyNode;
        ListNode fast  =dummyNode;

        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }

        while (fast.next!=null){
            slow = slow.next;
            fast = fast.next;
        }

        slow.next = slow.next.next;

        return dummyNode.next;
    }
}
